package com.chris.libs.base.x.mvpp.mvpp;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import androidx.annotation.StringRes;
import androidx.appcompat.app.AppCompatActivity;

import java.io.Serializable;
import java.util.Map;

/**
 * @author Chris chan
 * Create on： 2025/2/1 11:53
 * Use for:
 * Explain: 视图代理助手
 */
public interface ViewProxyAssist {
    Context requireContext();

    Activity requireActivity();

    /**
     * 跳转到其他的Activity
     * 适用于内部类
     *
     * @param packageContext
     * @param clazz
     */
    default void startActivityFromTo(Context packageContext, Class<?> clazz) {
        requireActivity().startActivity(new Intent(packageContext, clazz));
    }

    /**
     * 跳转到其他的Activity并结束
     * 适用于内部类
     *
     * @param clazz
     */
    default void startActivityFromToAndFinished(Context packageContext, Class<?> clazz) {
        requireActivity().startActivity(new Intent(packageContext, clazz));
        finish();
    }

    /**
     * 跳转到其他的Activity
     *
     * @param clazz
     */
    default void startActivity(Class<?> clazz) {
        requireActivity().startActivity(new Intent(requireContext(), clazz));
    }

    /**
     * 跳转到其他的Activity并且结束当前的Activity
     *
     * @param clazz
     */
    default void startActivityAndFinished(Class<?> clazz) {
        startActivity(clazz);
        finish();
    }

    /**
     * 跳转到其他的Activity并且携带数据
     *
     * @param clazz
     * @param key
     * @param value
     */
    default void startActivityWithExtra(Class<?> clazz, String key, Serializable value) {
        Intent intent = new Intent(requireContext(), clazz);
        intent.putExtra(key, value);
        requireActivity().startActivity(intent);
    }

    default void startActivityWithExtraMap(Class<?> clazz, Map<String, Serializable> map) {
        Intent intent = new Intent(requireContext(), clazz);
        for (Map.Entry<String, Serializable> entry : map.entrySet()) {
            intent.putExtra(entry.getKey(), entry.getValue());
        }
        requireActivity().startActivity(intent);
    }

    /**
     * 携带数据跳转到其他的Activity，并且结束当前Activity
     *
     * @param clazz
     * @param key
     * @param value
     */
    default void startActivityWithExtraAndFinished(Class<?> clazz, String key, Serializable value) {
        startActivityWithExtra(clazz, key, value);
        finish();
    }

    /**
     * 通过Intent对象跳转到其他Activity，并且结束当前Activity
     *
     * @param intent
     */
    default void startActivityIntentAndFinished(Intent intent) {
        requireActivity().startActivity(intent);
        finish();
    }

    /**
     * 跳转到其他Activity并处理返回结果
     *
     * @param clazz
     * @param requestCode
     */
    default void startActivityForResult(Class<?> clazz, int requestCode) {
        requireActivity().startActivityForResult(new Intent(requireContext(), clazz), requestCode);
    }

    /**
     * 跳转到其他Activity并处理返回结果
     *
     * @param clazz
     */
    default void startActivityForResult(Class<?> clazz) {
        requireActivity().startActivityForResult(new Intent(requireContext(), clazz), 0);
    }

    default void finish() {
        requireActivity().finish();
    }

    //******************************************
    @SuppressLint("ShowToast")
    default void showMessage(CharSequence info, int time) {
        Toast.makeText(requireContext(), info, time).show();
    }

    @SuppressLint("ShowToast")
    default void showMessage(CharSequence info) {
        Toast.makeText(requireContext(), info, Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("ShowToast")
    default void showMessageLong(CharSequence info) {
        Toast.makeText(requireContext(), info, Toast.LENGTH_LONG).show();
    }

    @SuppressLint("ShowToast")
    default void showMessage(@StringRes int resId, int time) {
        Toast.makeText(requireContext(), resId, time).show();
    }

    @SuppressLint("ShowToast")
    default void showMessage(@StringRes int resId) {
        Toast.makeText(requireContext(), resId, Toast.LENGTH_SHORT).show();
    }

    @SuppressLint("ShowToast")
    default void showMessageLong(@StringRes int resId) {
        Toast.makeText(requireContext(), resId, Toast.LENGTH_LONG).show();
    }
}
